<template>
  <view>
    <view class="search-box">
      <!-- 基本用法 -->
      <uni-search-bar @input="input" focus placeholder='请输入搜索内容' :radius='100' cancelButton='none' clearButton="always">
      </uni-search-bar>
    </view>

    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" @click="gotoDetail(item)" v-for="(item, i) in searchResults" :key="i">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>

    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
      </view>
      <!-- 列表 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 延时器变量
        time: null,
        // 搜索关键字
        kw: '',
        // 搜索结果列表
        searchResults: [],
        // 搜索关键词的历史记录
        historyList: []
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') 
    },
    methods: {
      // input 输入事件e
      input(value) {
        clearTimeout(this.time)
        this.time = setTimeout(() => {
          this.kw = value
          // 根据关键词，查询搜索建议列表
          this.getSearchList()
          console.log(value);
        }, 500)
      },
      // 获取搜索结果
      async getSearchList() {
        // 判断关键词是否为空
        if (this.kw === '') {
          this.searchResults = []
          return
        }
        // 判断关键词是否为空
        if (this.kw === '') {
          this.searchResults = []
          return
        }
        // 发起请求，获取搜索建议列表
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
        // 保存搜索关键字
        this.saveSearchList()
      },
      // 跳转详情页
      gotoDetail(item) {
        console.log(item.goods_id);
        uni.navigateTo({
          url: "/subpkg/goods-detail/goods-detail?goods_id=" + item.goods_id
        })

      },
      // 保存搜索历史
      saveSearchList(){
        const set = new Set(this.historyList)
        console.log(set);
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        // 持久化存储
        uni.setStorageSync('kw', JSON.stringify(this.historyList));
      },
      // 清除搜索历史
      cleanHistory(){
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      // 点击搜索历史跳转详细
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+item
        })
      }
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 10rpx;

    .sugg-item {
      font-size: 24rpx;
      padding: 26rpx 0;
      border-bottom: 2rpx solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-right: 6rpx;
      }
    }
  }

  .history-box {
    padding: 0 10rpx;
    .history-title {
      display: flex;
      justify-content: space-between;
      height: 80rpx;
      align-items: center;
      font-size: 26rpx;
      border-bottom: 2rpx solid #efefef;
    }
    .history-list {
     
      display: flex;
      flex-wrap: wrap;
      .uni-tag{ 
        margin-top: 10rpx;
        margin-right: 10rpx;
      }
    }
  }
</style>
